/**
 * related to list.ui
 * 
 * @Author : chenglm
 * @Timestamp : 2016-07-15
 */
var page = sm("do_Page");
var app = sm("do_App");
var hashdata = mm("do_HashData");
var rootview = ui("$");
var nf = sm("do_Notification");
var common = require("common");
var orderUtil = require("helper/order");
var pubUrl = require("url/pub");
var qqHttp = require("helper/http");
var layoutMain = ui("layoutMain");
var addItemArr = [];
var starArr = [];
var lStarText = 5; //物流星星
var deliveryContent = "";
var imgFilesArr = [];

//andriod返回键
page.on("back", function() {
	app.closePage({"feedback_add":true});
});

var headerAout = ui("do_ALayout_header");
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
//左上角-返回
var headerLeft = ui(addheader+".headerLeft");
headerLeft.on("touch", function() {
	app.closePage({"feedback_add":true});
});
var btnSubmit = ui(addheader + ".headerRight");
var labelNext = ui(addheader + ".labelNext");
labelNext.visible = true;
var header = ui("header");
header.bindData(hashdata);
hashdata.addData({
    "labelTitle" : "发表评价",
	"labelNext" : "提交"
})
header.refreshData();
//页面装载完成后，开始初始化工作
page.on("loaded", function(){
	refreshAllData();
});
//刷新数据
function refreshAllData(){
	var orderNo = page.getData().orderNo;
	qqHttp.post(pubUrl.url.feedback.toAdd, {orderNo:orderNo}, function(data) {
		var datass = data.itemList;
		if (datass.length) {
			var addLogistics = layoutMain.add("logistics", "source://view/order/comment/logisticsTemplate.ui", 0,0);
			for(var i=0,length = datass.length;i<length;i++) {
				var addItem = layoutMain.add("item"+i, "source://view/order/comment/listAddTemplate.ui", 0, 0);
				ui(addItem+".indexText").text = i;
				//设置数据绑定的映射关系
				ui(addItem+".do_ImageView_img").source= datass[i].imgItems[0].source;
				ui(addItem+".layoutDetail").tag = datass[i].orderNo;
				ui(addItem+".productImg").tag = datass[i].productId;
				ui(addItem+".layoutPics").tag = datass[i].orderType;
				addItemArr.push({productId:datass[i].id,addItemUi:addItem});
			}
			
		}
		
	})
}

page.on("starNum",function(data){
	starArr[data.index] = data.num;
});
page.on("lStarNum",function(data){
	lStarText = data.num;
	deliveryContent = data.content;
})
page.on("imgFiles0",function(data){
	if(data.imgFiles && data.imgFiles.length > 0){
		if(data.imgFiles.length>3){
			nf.toast("不能多于3张图片");
			return;
		}else{
			qqHttp.form(data.imgFiles,"pub/feedback",function(imgDatas){
				imgFilesArr[data.index] = imgDatas.path;
			});
		}
	}else{
		imgFilesArr[data.index] = "";
	}
})
btnSubmit.on("touch","",3000,function(){
	var flag = false;
	var productArr = [];
	var param ={};
	for (var i=0; i<addItemArr.length; i++) {
		var productItem = addItemArr[i];
		var orderNo = ui(productItem.addItemUi+".layoutDetail").tag;
		var productId = ui(productItem.addItemUi+".productImg").tag;
		var content = ui(productItem.addItemUi+".content").text.trim();
		var orderType = ui(productItem.addItemUi+".layoutPics").tag;
		var score = starArr[i]?starArr[i]:5;
		var string4imgs = imgFilesArr[i]?imgFilesArr[i]:"";
		if(content == ""){
			nf.toast("评价内容不能为空");
			return;
		}
		var addParams = {
            orderNo:orderNo,
            productId:productId,
			content:content,
			img:string4imgs,
			score:score
		};
		productArr.push(addParams);
		param.orderNo = orderNo;
		param.deliveryScore = lStarText;
		param.deliveryContent = deliveryContent;
	}
	param.feedbackList = productArr;
	qqHttp.post(pubUrl.url.feedback.doAdd2,param,function(data){
		nf.toast("成功添加评价");
		app.closePage({"feedback_add":true,"feedbackSuccess":true});
	},function(){
		btnSubmit.enabled = true;
	});
})
//点击背景隐藏输入键盘
rootview.on("touch",function(){
	page.hideKeyboard();
})
